#coding=utf8
import sys 
import os
import jieba
from matplotlib.pyplot import hist
import openpyxl
import pandas as pd
import numpy as np
import string                            #导入字符串模块
import threading
from collections import Counter
from docx import Document               # 导入doc文档所用库
from win32com import client as wc

class threadCutTXT(threading.Thread):
    # 输入参数 名称 id 总线程数
    # 输出参数 返回一个字典-词频的
    def __init__(self, filelocation, numId, numSum, staticmodul):
        threading.Thread.__init__(self)
        self.filelocation = filelocation
        self.numId = numId
        self.numSum = numSum
        self.finish = False
        self.staticmodul = staticmodul
        self.hist = {} #创建一个空字典，放词频与单词，无序排列
    # run方法
    def run(self):
        filetxt = open(self.filelocation,'r',encoding='utf-8')
        filetxtlins = filetxt.readlines()
        self.hist = {}
        num = 0

        for i in range(len(filetxtlins)):
            line = filetxtlins[i]
            if True:#num % self.numSum == self.numId:
                #print(num) #第i行
                line = line.replace('-',' ')       #连字符—用空格代替 
                words = line.split()               #字符串按空格分割--分词
                #迭代处理：将字典变列表，存入数据
                for i in range(len(words)):
                    words[i] = words[i].strip(string.punctuation)  #去掉标点符号，去掉首尾
                    if(self.staticmodul & 1 == 0): #末尾为1 模式为区分大小写为0就是不区分
                        words[i] = words[i].lower()                    #统一大小写
                    if words[i] in self.hist:                          #统计词频与单词
                        self.hist[words[i]] = self.hist[words[i]] + 1        #不是第一次
                    else:
                        self.hist[words[i]] = 1                         #第一次
            num += 1
        self.finish = True


    def getStatic(self):
        return self.finish

    def getHist(self):
        return self.hist
        

class threadCutDOCX(threading.Thread):
    # 输入参数 名称 id 总线程数 staticmodul: 1为区分大小写
    # 输出参数 返回一个字典-词频的
    def __init__(self, filelocation, numId, numSum, staticmodul):
        threading.Thread.__init__(self)
        self.filelocation = filelocation
        self.numId = numId
        self.numSum = numSum
        self.finish = False
        self.staticmodul = staticmodul
        self.hist = {}#创建一个空字典，放词频与单词，无序排列
    # run方法
    def run(self):
        filedoc = Document(self.filelocation)
        self.hist = {} 
        num = 0
        for para in filedoc.paragraphs:
            line = para.text
            if True:#num % self.numSum == self.numId :# 就一个线程先这样做吧
                #print(num) #第i行
                line = line.replace('-',' ')       #连字符—用空格代替
                words = line.split()               #字符串按空格分割--分词
                #迭代处理：将字典变列表，存入数据
                for i in range(len(words)):
                    words[i] = words[i].strip(string.punctuation)  #去掉标点符号，去掉首尾
                    if(self.staticmodul & 1 == 0): #末尾为1 模式为区分大小写为0就是不区分
                        words[i] = words[i].lower()                    #统一大小写
                    if words[i] in self.hist:                          #统计词频与单词
                        self.hist[words[i]] = self.hist[words[i]] + 1        #不是第一次
                    else:
                        self.hist[words[i]] = 1                         #第一次
            num += 1 
        self.finish = True

    def getStatic(self):
        return self.finish
        
    def getHist(self):
        return self.hist

class Getword():
    # 输入参数 文件夹的列表并计算
    def openFile(self, filelocationList, staticmodul):
        # 识别是txt 还是doc
        self.histSum = {}
        self.numsum = 0
        for i in range(len(filelocationList)):

            houzhui = filelocationList[i].split('.')[-1]
            
            if(houzhui == 'txt'): # 是txt--开线程去切
                self.threadcut = threadCutTXT(filelocationList[i], 0, 0, staticmodul)
                self.threadcut.start()

            elif houzhui == 'docx': # 是doc--开线程去切
                self.threadcut = threadCutDOCX(filelocationList[i], 0, 0, staticmodul)
                self.threadcut.start()
            elif (houzhui == 'doc')|(houzhui == 'wps')|(houzhui == 'wpt')|(houzhui == 'dot')|(houzhui == 'rtf')|(houzhui == 'docx')|(houzhui == 'dotx')|(houzhui == 'docm')|(houzhui == 'dotm')|(houzhui == 'xml')|(houzhui == 'mht')|(houzhui == 'mhtml')|(houzhui == 'html')|(houzhui == 'htm')|(houzhui == 'pdf'): # 利用wps应用接口进行转换成doc再接着用doc操作进行去玩
                try:
                    print("开始转换")
                    word = wc.gencache.EnsureDispatch('kwps.application')# 先转换为docx再去操作
                    doc = word.Documents.Open(filelocationList[i])        # 目标路径下的文件
                    doc.SaveAs(os.getcwd()+'\\getch.docx',12, False, "", True, "", False, False, False, False)  # 转化后路径下的文件    
                    doc.Close()
                    word.Quit()
                    print("转换完毕")
                    self.threadcut = threadCutDOCX(os.getcwd()+'\\getch.docx', 0, 0, staticmodul)
                    self.threadcut.start()
                except:
                    print('请安装wps或将wps更新到最新版本')
        
    def getHistList(self):
        if(self.threadcut.getStatic() == True):
            X,Y = Counter(self.histSum), Counter(self.threadcut.getHist())
            self.histSum = dict(X+Y)
            return True,self.histSum
        else:
            return False,{}

